import java.util.Arrays;
import java.util.PriorityQueue;

public class Test {
    public static void main(String[] args) {
        int[] arr={1,44,33,55,22};
        int k=3;
        System.out.println(Arrays.toString(findMax(arr,k)));
    }
    public static void main2(String[] args) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>();//默认小根堆
        priorityQueue.offer(22);
        priorityQueue.offer(12);
        priorityQueue.offer(30);
        priorityQueue.offer(1);
        System.out.println(priorityQueue.peek());
    }
    public static void main1(String[] args) {
        TestHeap testHeap=new TestHeap();
        int[] array={27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);
        testHeap.createHeap();
        testHeap.offer(80);
        testHeap.poll();
    }
    //找出数组中最大的k个元素
    public static int[] findMax(int[] array,int k){
        PriorityQueue<Integer> heap=new PriorityQueue<>();
        for(int i=0;i<k;i++){
            heap.offer(array[i]);
        }
        for(int i=k;i<array.length;i++){
            int tmp=heap.peek();
            if(tmp<array[i]){
                heap.poll();
                heap.offer(array[i]);
            }
        }
        int[] res=new int[k];
        for(int i=0;i<k;i++){
            res[i]=heap.poll();
        }
        return res;
    }
}